#include "write_score.h"

WriteScore* WriteScore::ptr = NULL;

WriteScore::WriteScore (Champions g[3], int s, int t)
{
	ptr = this;
	for (int i = 0; i < 3; i ++)
		gamer[i] = g[i];
	steps = s;
	time = t;
}

BOOL CALLBACK WriteScore::DlgProc(HWND hWnd, UINT mes, WPARAM wParam, LPARAM lParam)
{
	switch(mes)
	{
		HANDLE_MSG(hWnd,WM_INITDIALOG,ptr->Cls_OnInitDialog);
		HANDLE_MSG(hWnd,WM_CLOSE,ptr->Cls_OnClose);
		HANDLE_MSG(hWnd,WM_COMMAND,ptr->Cls_OnCommand);
	}
	return FALSE;
}

BOOL WriteScore::Cls_OnInitDialog (HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
	return TRUE;
}

void WriteScore::Cls_OnClose (HWND hWnd)
{
	EndDialog(hWnd, 0);
}

void WriteScore::Cls_OnCommand (HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
	if (id == IDOK)
	{
		TCHAR name[20];
		GetWindowText(GetDlgItem(hWnd, IDC_EDIT1), name, 20);
		if (steps < gamer[0].steps || (steps == gamer[0].steps && time < gamer[0].time))
		{
			lstrcpy(gamer[2].name, gamer[1].name);
			gamer[2].steps = gamer[1].steps;
			gamer[2].time = gamer[1].time;
			lstrcpy(gamer[1].name, gamer[0].name);
			gamer[1].steps = gamer[0].steps;
			gamer[1].time = gamer[0].time;
			lstrcpy(gamer[0].name, name);
			gamer[0].steps = steps;
			gamer[0].time = time;
		}
		else if (steps < gamer[1].steps || (steps == gamer[1].steps && time < gamer[1].time))
		{
			lstrcpy(gamer[2].name, gamer[1].name);
			gamer[2].steps = gamer[1].steps;
			gamer[2].time = gamer[1].time;
			lstrcpy(gamer[1].name, name);
			gamer[1].steps = steps;
			gamer[1].time = time;
		}
		else if (steps < gamer[2].steps || (steps == gamer[2].steps && time < gamer[2].time))
		{
			lstrcpy(gamer[1].name, name);
			gamer[1].steps = steps;
			gamer[1].time = time;
		}
		
		Game::ptr->gamer[0] = gamer[0];
		Game::ptr->gamer[1] = gamer[1];
		Game::ptr->gamer[2] = gamer[2];

		EndDialog(hWnd, 0);
	}

	else if (id == IDCANCEL)
	{
		EndDialog(hWnd, 0);
	}
}